package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class AdSensitiveServiceImpl extends ServiceImpl <AdSensitiveMapper, AdSensitive>implements AdSensitiveService {

    @Override
    public ResponseResult list(SensitiveDto dto) {

        //检查参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();

//迷糊分页
        Page page = new Page<>(dto.getPage(),dto.getSize());

        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  //判断名字不能为空
         if (StringUtils.isNotBlank(dto.getName())){
             lambdaQueryWrapper.like(AdSensitive::getSensitives,dto.getName());
         }

        IPage result = page(page, lambdaQueryWrapper);



        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) result.getTotal());

        pageResponseResult.setData(result.getRecords());

        return  pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        adSensitive.setCreatedTime(new Date());

         save(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if (adSensitive==null || adSensitive.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

         updateById(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult deleteById(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdSensitive adSensitive=getById(id);
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            //错误码数据不存在
        }
        removeById(id);

    return  ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
